package com.jinbo.sxzq;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.Log;

/*************棋子父类***************/
public class Chess {
    public String name;//棋子名字
    public String message;//棋子信息
    public int index;//棋子的编号
    public int type;//棋子的类型(类型越小行动顺序越靠前)

    public int maxHp;//棋子初始生命
    public int hp;//棋子生命
    public int maxMp;//棋子最大能力值
    public int mp;//棋子当前能力值
    public int mobility;//棋子的行动力
    public int maxmobility;//棋子的最大行动力
    public int atk;//棋子攻击力
    public int[] location;//棋子当前位置

    public int state;//棋子状态
    public int owner;//棋子的所属权

    public Bitmap chessmip;//棋子图片
    public GameControl gameControl;//棋盘

    Chess(int index , int[] location , int owner , GameControl gameControl){
        this.index = index;
        this.location = location;
        this.owner = owner;
        this.state = ProjectConstants.ChessState.DEPLOY;
        this.gameControl = gameControl;
        this.mp = 0;
    }

    /*棋子行动的方法
      棋子技能可触发则触发技能
      之后棋子开始行动 移动/攻击
     */
    public void Action(){
        float minlength = 100;//离自己最近的敌方棋子的距离
        int aimindex = -1;//目标棋子的索引位置
        if(this.mp >= this.maxMp){//触发技能
            Skill();
        }
        for(int i = 0 ; i < gameControl.chess_list.size() ; i++){//找到离自己最近的敌方棋子
            if(gameControl.chess_list.get(i).getOwner(this) != owner){
                Chess chess = gameControl.chess_list.get(i);
                float length = Math.max(Math.abs(chess.location[0]-location[0]),Math.abs(chess.location[1]-location[1]));
                if(minlength > length){
                    minlength = length;
                    aimindex = i;
                }
            }
        }
        if(aimindex != -1){
            if(minlength == 1){
                Attack(gameControl.chess_list.get(aimindex));
                Log.e("Action","攻击");
            }else{
                Move(gameControl.chess_list.get(aimindex));
                Log.e("Action","移动");
            }
        }else{
            Log.e("Action","未选中目标棋子");
            mobility--;
        }
    }

    public void Skill(){//触发棋子技能的方法
        mp = 0;
    }

    public void Move(Chess chess){//移动操作
        int chessx = chess.location[0];
        int chessy = chess.location[1];
        mobility--;
        gameControl.setBoard(location[0],location[1],ProjectConstants.ChessType.KONG);
        gameControl.setBoardOwner(location[0],location[1],ProjectConstants.ChessOwner.EMPTY);
        //上下移动
        if(location[0] > chessx){//向上移动
            if(gameControl.getBoard()[location[0]-1][location[1]] == 0){
                location[0]--;
            }
        }
        if(location[0] < chessx){//向下移动
            if(gameControl.getBoard()[location[0]+1][location[1]] == 0){
                location[0]++;
            }
        }
        //左右移动
        if(location[1] > chessy){//向左移动
            if(gameControl.getBoard()[location[0]][location[1]-1] == 0){
                location[1]--;
            }
        }
        if(location[1] < chessy){//向右移动
            if(gameControl.getBoard()[location[0]][location[1]+1] == 0){
                location[1]++;
            }
        }
        gameControl.setBoard(location[0],location[1],type);
        gameControl.setBoardOwner(location[0],location[1],owner);
    }

    public void Attack(Chess chess){//攻击目标棋子
        chess.hp = chess.hp - this.atk;
        mobility = 0;
        if(chess.hp <= 0){
            this.Kill(chess);
        }
    }

    public void RoundNext(){//进入下一回合
        mobility = maxmobility;//恢复行动力
        mp++;//恢复1能力值
    }

    public void Kill(Chess chess){//击杀目标棋子
        for(int i = 0 ; i < gameControl.chess_list.size() ; i++){
            if(gameControl.chess_list.get(i).index == chess.index){//棋子被击杀
                chess.state = ProjectConstants.ChessState.DEATH;
                gameControl.setBoard(chess.location[0],chess.location[1],ProjectConstants.ChessType.KONG);
                gameControl.chess_list.remove(i);
            }
        }
        gameControl.sum_deploy--;
        if(chess.owner == ProjectConstants.ChessOwner.OUR){
            gameControl.num_deploy--;
            gameControl.death_num_o++;
        }else{
            gameControl.death_num_e++;
        }
    }

    public int getOwner(Chess chess){
        return owner;
    }

}
